<template>
  <div>
    <el-col :span="24">
      <el-col :span="8">
        <el-input v-model="newPosition" placeholder="+添加职位"></el-input>
      </el-col>
      <el-col :span="4">
        <el-button :span="4" type="primary" @click="addPosition()"
          >+添加</el-button
        >
      </el-col>
    </el-col>

    <el-table
      ref="multipleTable"
      :data="positions"
      tooltip-effect="dark" 
      style="width: 100%"
    >
      <el-table-column type="selection" width="80"></el-table-column>
      <el-table-column prop="id" label="编号" width="100"></el-table-column>
      <el-table-column
        prop="name"
        label="职位名称"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="createDate"
        label="创建时间"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="enabled"
        label="是否启用"
        width="180"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="dialogPosition(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            :title="'此操作将永久删除，确定删除[' + scope.row.name + ']吗？'"
            @onConfirm="deletePosition(scope.row.id)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑职位下拉框 -->
    <el-dialog title="修改职位" :visible.sync="dialogFormPosition">
      <el-form :model="upPosition">
        <el-form-item label="职位名称" >
          <el-input v-model="upPosition.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" >
          <el-switch v-model="value" active-text="启用" inactive-text="禁用">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormPosition = false">取 消</el-button>
        <el-button type="primary" @click="updatePosition()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Position",
  data() {
    return {
      positions: [],
      upPosition: {
        id: "",
        name: "",
        enabled: "",
      },
      newPosition: "",
      value: true,
      dialogFormPosition: false,
    };
  },
  created() {
    this.getAllPosition();
  },
  methods: {
    getAllPosition() {
      this.$axios
        .post("basic/PositionController/getAllPosition", this.$qs.stringify({}))
        .then((value) => {
          for (let i = 0; i < value.data.length; i++) {
            if (value.data[i].enabled == "1") {
              value.data[i].enabled = "已启用";
            } else {
              value.data[i].enabled = "未启用";
            }
          }
          this.positions = value.data;
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 删除职位
    deletePosition(id) {
      this.$axios
        .post(
          "basic/PositionController/deletePositionById",
          this.$qs.stringify({ id: id })
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
          } else {
            alert("删除失败");
          }

          this.getAllPosition();
        })
        .catch((value) => {
          if (value != null) {
            this.$message({
              message: "该数据含有关联数据，删除失败",
              type: "error",
            });
          }
        });
    },
    dialogPosition(row) {
      this.dialogFormPosition = true;
      this.upPosition.name = row.name;
      this.upPosition.id = row.id;
      if (row.enabled == "未启用") {
        this.value = false;
      } else {
        this.value = true;
      }
    },
    // 添加职位
    addPosition() {
      this.$axios
        .post(
          "basic/PositionController/addPosition",
          this.$qs.stringify({ name: this.newPosition })
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
          } else {
            alert("添加失败");
          }
          this.newPosition = "";
          this.getAllPosition();
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 修改职位
    updatePosition() {
      this.dialogFormPosition = false;

      if (this.value == false) {
        this.upPosition.enabled = 0;
      } else {
        this.upPosition.enabled = 1;
      }
      this.$axios
        .post(
          "basic/PositionController/updatePosition",
          this.$qs.stringify(this.upPosition)
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "更新成功",
              type: "success",
            });
          } else {
            alert("更新失败");
          }
          this.getAllPosition();
        })
        .catch((value) => {
          console.error(value);
        });
    },
  },
};
</script>